package com.wushi.lenovo.asia5b.app_widget

import android.content.Context
import android.support.v7.widget.AppCompatTextView
import android.util.AttributeSet
import com.mvp.utils.DisplayUtils
import com.wushi.lenovo.asia5b.app_config.dddBug

/**
 * Created by zhangyuncai on 2019/7/16.
 * 用于订单button
 */
class OrderTextView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
    //adapter的item个数
    val adapterItemCount = 3

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec)
        //屏幕宽度
        val screenWidth = DisplayUtils.getScreenWidthAndHight(context)[0]
        //RecyclerView的最大宽度
        val recyclerViewMaxWidth = screenWidth - 2 * (DisplayUtils.dip2px(context, 16f))
        //预备宽度+左右padding
        val textViewPaddingRight = 0
        val textViewMarginLeft = DisplayUtils.dip2px(context, 20f)
        var yubeiWidth = measuredWidth + 2 * textViewPaddingRight
        //(如果预备宽度+margin宽度)*adapterItemCount>recyclerViewMaxWidth,那么就重新修改宽度
        val isDayu = ((yubeiWidth + textViewMarginLeft) * adapterItemCount) > recyclerViewMaxWidth
        dddBug("isDayu:$isDayu")
        if (isDayu) {//先计算控件宽度,如果三个控件宽度超过屏幕,那么重新修改宽度
            yubeiWidth = (recyclerViewMaxWidth.toFloat() / adapterItemCount - textViewMarginLeft).toInt()
        }
        setMeasuredDimension(yubeiWidth, measuredHeight)
        dddBug("measuredWidth:$measuredWidth")

    }
}